<template>
  <div class="supplier-file-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>供应商文件管理示例</span>
        </div>
      </template>

      <!-- 供应商基本信息 -->
      <div class="supplier-info" v-if="supplierDetail">
        <h3>{{ supplierDetail.supplierName }}</h3>
        <p>编码：{{ supplierDetail.supplierCode }}</p>
        <p>联系人：{{ supplierDetail.contactPerson }}</p>
      </div>

      <el-divider />

      <!-- Logo 文件管理 -->
      <BusinessFileManager
        title="Logo 文件"
        :business-id="supplierId"
        business-type="supplier"
        file-type="logo"
        accept=".jpg,.jpeg,.png,.gif"
        :max-size="5"
        :file-info="supplierDetail?.logoFile"
        @file-changed="handleFileChanged"
        @file-removed="handleFileRemoved"
      />

      <el-divider />

      <!-- 营业执照文件管理 -->
      <BusinessFileManager
        title="营业执照"
        :business-id="supplierId"
        business-type="supplier"
        file-type="license"
        accept=".pdf,.jpg,.jpeg,.png"
        :max-size="10"
        :file-info="supplierDetail?.licenseFile"
        @file-changed="handleFileChanged"
        @file-removed="handleFileRemoved"
      />

      <el-divider />

      <!-- 资质证书文件管理 -->
      <BusinessFileManager
        title="资质证书"
        :business-id="supplierId"
        business-type="supplier"
        file-type="certificate"
        accept=".pdf,.jpg,.jpeg,.png,.doc,.docx"
        :max-size="10"
        :file-info="supplierDetail?.certificateFile"
        @file-changed="handleFileChanged"
        @file-removed="handleFileRemoved"
      />
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {useRoute} from 'vue-router'
import {ElMessage} from 'element-plus'
import BusinessFileManager from '@/components/BusinessFileManager.vue'
import {getSupplierById, linkFileToSupplier, removeSupplierFile} from '@/api/supplier'

const route = useRoute()
const supplierId = ref(route.params.id)
const supplierDetail = ref(null)

// 加载供应商详情
const loadSupplierDetail = async () => {
  try {
    const response = await getSupplierById(supplierId.value)
    supplierDetail.value = response.data
  } catch (error) {
    ElMessage.error('加载供应商详情失败：' + error.message)
  }
}

// 处理文件变更
const handleFileChanged = async (fileData) => {
  try {
    // 调用接口关联文件到供应商
    await linkFileToSupplier(supplierId.value, {
      fileId: fileData.fileId,
      fileType: fileData.fileType
    })
    
    // 重新加载详情
    await loadSupplierDetail()
    
    ElMessage.success('文件关联成功')
  } catch (error) {
    ElMessage.error('文件关联失败：' + error.message)
  }
}

// 处理文件删除
const handleFileRemoved = async (fileType) => {
  try {
    // 调用接口删除文件关联
    await removeSupplierFile(supplierId.value, fileType)
    
    // 重新加载详情
    await loadSupplierDetail()
    
    ElMessage.success('文件删除成功')
  } catch (error) {
    ElMessage.error('文件删除失败：' + error.message)
  }
}

onMounted(() => {
  loadSupplierDetail()
})
</script>

<style scoped>
.supplier-file-management {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.supplier-info h3 {
  margin: 0 0 10px 0;
  color: #303133;
}

.supplier-info p {
  margin: 5px 0;
  color: #606266;
}
</style>